package cate.game.attr;

import cate.common.table.item.furniture.row.FurnitureItemBaseRow;
import cate.common.table.item.furniture.row.FurnitureLevelUpRow;
import cate.common.table.item.furniture.row.FurnitureRandomAttrRow;
import cate.common.table.item.hero.row.HeroBaseRow;
import cate.game.play.support.PlayBuildContext;
import cate.game.role.bag.furniture.Furniture;
import cate.game.role.bag.furniture.FurnituresOn;
import cate.game.role.bag.hero.Hero;


public class FurniturePowerAttrPart extends PowerAttrPart {

    public FurniturePowerAttrPart() {}

    public FurniturePowerAttrPart(PlayBuildContext ctx, FurnituresOn po) {
        if (po == null) {
            return;
        }
        FightAttr attr = new FightAttr();
        Hero hero = po.owner;
        if(hero != null) {
            HeroBaseRow heroBaseRow = hero.getTpl(ctx);
            byte camp = heroBaseRow.camp;
            byte job = heroBaseRow.job;
            if (po.furnitureMap != null) {
                for (Furniture furniture : po.furnitureMap.values()) {
                    FurnitureItemBaseRow baseRow = furniture.getTpl(ctx);
                    if (baseRow == null) {
                        return;
                    }
                    FurnitureLevelUpRow levelUpRow = null;
                    if (furniture.level > 0) {
                        levelUpRow = ctx.furnitureExcel().levelUp.findRow(baseRow.protoId, furniture.level);
                    }
                    if (job == furniture.job && furniture.job1AttrId > 0) {
                        FurnitureRandomAttrRow jobAttrRow = ctx.furnitureExcel().randomAttr.get(furniture.job1AttrId);
                        if (jobAttrRow != null) {
                            FightAttr jobAttr = new FightAttr(jobAttrRow.attrStr);
                            if (levelUpRow != null) {
                                jobAttr.scale(1 + levelUpRow.job1Add / 10000d);
                            }
                            attr.add(jobAttr);
                        }
                    }
                    if (job == furniture.job && furniture.job2AttrId > 0) {
                        FurnitureRandomAttrRow job2AttrRow = ctx.furnitureExcel().randomAttr.get(furniture.job2AttrId);
                        if (job2AttrRow != null) {
                            FightAttr jobAttr = new FightAttr(job2AttrRow.attrStr);
                            if (levelUpRow != null) {
                                jobAttr.scale(1 + levelUpRow.job2Add / 10000d);
                            }
                            attr.add(jobAttr);
                        }
                    }
                    if (furniture.commonAttrId > 0) {
                        FurnitureRandomAttrRow commonAttrRow = ctx.furnitureExcel().randomAttr.get(furniture.commonAttrId);
                        if (commonAttrRow != null) {
                            FightAttr jobAttr = new FightAttr(commonAttrRow.attrStr);
                            if (levelUpRow != null) {
                                jobAttr.scale(1 + levelUpRow.commonAdd / 10000d);
                            }
                            attr.add(jobAttr);
                        }
                    }
                    if (camp == furniture.camp && furniture.camp1AttrId > 0) {
                        FurnitureRandomAttrRow camp1AttrRow = ctx.furnitureExcel().randomAttr.get(furniture.camp1AttrId);
                        if (camp1AttrRow != null) {
                            FightAttr jobAttr = new FightAttr(camp1AttrRow.attrStr);
                            if (levelUpRow != null) {
                                jobAttr.scale(1 + levelUpRow.camp1Add / 10000d);
                            }
                            attr.add(jobAttr);
                        }
                    }
                    if (camp == furniture.camp && furniture.camp2AttrId > 0) {
                        FurnitureRandomAttrRow camp2AttrRow = ctx.furnitureExcel().randomAttr.get(furniture.camp2AttrId);
                        if (camp2AttrRow != null) {
                            FightAttr jobAttr = new FightAttr(camp2AttrRow.attrStr);
                            if (levelUpRow != null) {
                                jobAttr.scale(1 + levelUpRow.camp2Add / 10000d);
                            }
                            attr.add(jobAttr);
                        }
                    }
                }
            }
        }
        this.addAttr(attr);
        this.skills.addAll(po.getSkills(ctx));
    }
}
